package main;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import lombok.SneakyThrows;
import utils.SmallTool;
import utils.StringUtil;

import java.util.List;

/**
 * @Author: gcr
 * @Date: 2022-3-22 17:09
 */
public class comparedoifinal {
    public static String myDoiSource = "compare_doi";

    @SneakyThrows
    public static void main(String[] args) {
        //这个程序主要比对我的和老板都不为空的情况下的doi
        SmallTool.printMemory();
        Db useMe = Db.use();
        String allDate = "select max(pmid) as pmid  from " + myDoiSource;
        Entity query = useMe.query(allDate).get(0);
        SmallTool.printMess("最大的id 是" + query.getStr("pmid"));
        Integer count = query.getInt("pmid");
        int breakSize = 100000;
        int pageSize = count / breakSize + 1;
        SmallTool.printMess("全部页码+++++++++" + pageSize);
        SmallTool.printMess("任务开始+++++++++++++++++++++++++++++++++++++++++++++++++++++");
        for (int i = 1; i <= pageSize; i++) {
            SmallTool.printMess("当前页码+++++" + i);
            SmallTool.printMemory();
            int start = breakSize * (i - 1) + 1;
            int end = i * breakSize;
            SmallTool.printMess("开始id++" + start + "+++结束id++++" + end);
            String paramSql = "select pmid,doi_new,doi_boss from " + myDoiSource + " where  pmid between  " + start + " and " + end;
            List<Entity> querys = useMe.query(paramSql);

            querys.parallelStream().forEach(entity -> {
                Integer pmid = entity.getInt("pmid");
                String doiNew = entity.getStr("doi_new");
                String doiBoss = entity.getStr("doi_boss");
                String doiFinal = "";
                if (StringUtil.isNull(doiNew) && StringUtil.isNotNull(doiBoss)) {
                    doiFinal = doiBoss;
                }
                if (StringUtil.isNull(doiBoss) && StringUtil.isNotNull(doiNew)) {
                    doiFinal = doiNew;
                }
                if (StringUtil.isNotNull(doiNew) && StringUtil.isNotNull(doiBoss) && !doiNew.equals(doiBoss)) {
                    doiFinal = doiBoss;
                }
                if (StringUtil.isNotNull(doiNew) && StringUtil.isNotNull(doiBoss) && doiNew.equals(doiBoss)) {
                    doiFinal = doiNew;
                }
                // 进行更新到最新的doi上面去
                insertData(pmid, doiFinal, useMe);

            });
        }

    }

    @SneakyThrows
    public static void insertData(Integer pmid, String doi, Db use) {
        Entity whereEn = new Entity();
        whereEn.setTableName(myDoiSource);
        whereEn.set("pmid", pmid);
        use.update(new Entity()
                .set("doi_right", doi), whereEn
        );

    }


}
